//
//  CRSaveTool.m
//  NewAge
//
//  Created by dllo on 15/12/5.
//  Copyright © 2015年 ColorRevolution. All rights reserved.
//

#import "CRSaveTool.h"

@implementation CRSaveTool

#warning 3. 用来判断文件夹是否存在,如果没有文件夹,就创建一个,如果有不做任何操作.
+ (void)isHaveNewsFolder {
    // 1. 沙盒路径
    
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // 2. 拼接电影的文件夹名
    NSString *modelFile = [sandBoxPath stringByAppendingPathComponent:@"/News"];
    // 创建一个文件的管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    // 通过管理者调用方法, 根据路径判断文件夹是否存在
    if (![manager fileExistsAtPath:modelFile]) {
        // 创建文件夹
        [manager createDirectoryAtPath:modelFile withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSLog(@"%@", modelFile);
    
}

#warning 4.因为按钮上得字显示的是当前model的收藏情况,所以需要先进行反归档,如果存在就显示已收藏,不存在显示收藏
+ (BOOL)isHaveModelInPlist:(CRNewsModel *)news {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"News/news.plist"];
    // 反归档
    NSArray *modelArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    // 遍历
    for (CRNewsModel *tempModel in modelArr) {
        if ([tempModel.url_3w isEqual:news.url_3w]) {
            return NO;
        }
    }
    return YES;
}

#warning 5. 把数据存到对应的文件中
+ (void)saveModelToPlist:(CRNewsModel *)news {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"News/news.plist"];
    //反归档
    //直接归档会覆盖掉之前的数据,所以先进行反归档,然后添加进去之后在进行归档处理操作
    NSMutableArray *modelArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    if (!modelArr) {
        // 如果为空, 先进行初始化
        modelArr = [NSMutableArray array];
    }
    [modelArr addObject:news];
    
    // 归档
    [NSKeyedArchiver archiveRootObject:modelArr toFile:docPath];
    NSLog(@"%@", docPath);
    
}

#warning 6. 取消收藏的方法
+ (void)cancelModelInPlist:(CRNewsModel *)news {
    // 写路径
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"News/news.plist"];
    
    // 反归档
    NSMutableArray *modelArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    // 遍历之后移除相应对象
    for (CRNewsModel *temp in modelArr) {
        if ([temp.url_3w isEqual:news.url_3w]) {
            // 移除
            [modelArr removeObject:temp];
            break;
        }
    }
    // 归档
    [NSKeyedArchiver archiveRootObject:modelArr toFile:docPath];
}

+ (BOOL)isHaveInFile:(id)object type:(SaveToolType)type {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    switch (type) {
        case SaveToolSpecialNews: {
            NSString *docPath = [sandBoxPath stringByAppendingString:@"News/news.plist"];
            NSArray *movieArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            CRNewsModel *model = object;
            for (CRNewsModel *temp in movieArr) {
                if ([temp.skipType isEqual:model.skipType = @"special"]) {
                    return NO;
                }
            }
        }
            break;
        case SaveToolPic: {
            NSString *docPath = [sandBoxPath stringByAppendingString:@"News/news.plist"];
            NSArray *movieArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            CRNewsModel *model = object;
            for (CRNewsModel *temp in movieArr) {
                if ([temp.imgextra isEqual:model.imgextra]) {
                    return NO;
                }
            }
        }
            break;
        case SaveToolNews: {
            NSString *docPath = [sandBoxPath stringByAppendingString:@"News/news.plist"];
            NSArray *movieArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            CRNewsModel *model = object;
            for (CRNewsModel *temp in movieArr) {
                if ([temp.imgextra isEqual:model.imgextra]) {
                    return NO;
                }
            }
        }
            break;
        default:
            break;
    }
    return YES;

}



@end
